import axios, { AxiosError, AxiosRequestConfig } from "axios"
import { message } from "antd"

const instance = axios.create({
    baseURL: 'http://127.0.0.1:5000',
    timeout: 10000,
    withCredentials: true,
})

export default function request<R>(
    options: AxiosRequestConfig
): Promise<R> {
    return instance(options)
        .then((res) => {
            const data: R = res.data || {}
            return Promise.resolve(data)
        })
        .catch((err: AxiosError) => {
            let msg = ''

            if (err.response) {
                msg = err.response.statusText
            } else if (err.request) {
                msg = err.request.response
            } else {
                msg = err.message
            }

            msg = msg || '请求异常，请稍后尝试'
            console.log('Error msg:', msg)
            message.info(msg)

            return Promise.reject(msg)
        })
}